package blog

import (
	"fmt"
	"strings"
)

type STAGE int

func (s STAGE) String() string {
	return STAGE_MAPPING[s]
}

// 自定义 类型的序列化方式
// "草稿"
func (s STAGE) MarshalJSON() ([]byte, error) {
	return []byte(`"` + s.String() + `"`), nil
}

// UnmarshalJSON([]byte) error
func (s *STAGE) UnmarshalJSON(data []byte) error {
	str := strings.Trim(string(data), `"`)
	switch str {
	case "草稿":
		*s = STAGE_DRAFT
		// v := STAGE_DRAFT
		// s = &v
	case "已发布":
		*s = STAGE_PUBLISHED
	default:
		return fmt.Errorf("不支持的发布类型")
	}
	return nil
}

var STAGE_MAPPING = map[STAGE]string{
	STAGE_DRAFT:     "草稿",
	STAGE_PUBLISHED: "已发布",
}

const (
	STAGE_DRAFT STAGE = iota
	STAGE_PUBLISHED
)
